//
//  ViewController.m
//  iOS
//
//  Created by mac on 16-4-19.
//  Copyright (c) 2016年 wangchenglong. All rights reserved.
//

#import "ViewController.h"
@interface PhoneData : NSObject
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *phone;
@end
@implementation PhoneData

@end
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    
    NSMutableArray*data;
}

@end

@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
//    data =@[
//            @"将乐琦说：将乐琦是猪",
//            @"胖子说：是滴",
//            @"龙爸爸说：我同意",
//            @"将乐琦说：谢谢龙爸爸"
//            ];
    self.title=@"将乐琦电话簿";
    UITableView *tableview=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain ];
    tableview.dataSource=self;
    tableview.delegate=self;
    [self.view addSubview:tableview];
    
    
 /*   NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"tel" withExtension:@""];
    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：%@", text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    NSMutableArray *tmp = [NSMutableArray new];
    for (int i = 0; i < arr.count; i++) {
        NSString *item = arr[i];
        NSArray *p = [item componentsSeparatedByString:@","];
        Person *per = [Person new];
        per.name = p[0];
        per.tel = p[1];
        [tmp addObject:per];
    }
    data = tmp;*/
    
    
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"00001" withExtension:@"vcf"];
    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取数据：%@", text);
    
    NSArray *arr = [text componentsSeparatedByString:@"\n"];
    PhoneData*item;
    NSMutableArray *tmp = [NSMutableArray new];
    for(NSString*p in arr){
    if([p hasPrefix:@"BEGIN"])
    {
        item=[PhoneData new];
    }else if([p hasPrefix:@"END"])
    {
        [tmp addObject:item];
    }else if([p hasPrefix:@"TEL"])
    {
    
    NSArray *components=[p componentsSeparatedByString:@":"];
        NSString*app=components[1];
        item.phone=app;
    }
    else if([p hasPrefix:@"FN:"]){
        NSArray *component=[p componentsSeparatedByString:@":"];
        NSString*abb=component[1];
        item.name=abb;
    }
        data=tmp;
    }
}
    
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // return 10;
    return data.count;
  
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //UITableViewCell*call=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    //call.textLabel.text=data[indexPath.row];
    
    
  //  UITableViewCell*call=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
  //  Person *p = data[indexPath.row];
  //  call.textLabel.text = p.name;
  //  call.detailTextLabel.text = p.tel;
    
    
    
    UITableViewCell*call=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
      PhoneData *p = data[indexPath.row];
      call.textLabel.text = p.name;
      call.detailTextLabel.text = p.phone;
    return call;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PhoneData*d=data[indexPath.row];
    NSString*phone=[NSString stringWithFormat:@"tel://%@",d.phone];
    NSURL*url=[NSURL URLWithString:phone];
    UIApplication*zxc=[UIApplication sharedApplication];
    [zxc openURL:url];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
